/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!				\file		CC_Layer.cc
 \date		june 2010
 \author		TNick

 \brief		Implements CC_Layer class


	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Please read ReadMe.txt and Licence.txt in root folder
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"../main.h"

#include	<assert.h>

#include	"CC_App.h"
#include	"CC_Layer.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */



/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
/* actual constructor	                                                     */
CCERR::EERRORS		CC_Layer::create		(CC_Draw* pDraw, CC_Layer* pTpl,
											 QString strNewName,
											 CC_Layer** pOut, CC_Err* pErr)
	{

	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;

	/* first, create empty the object in memory */
	CC_Layer*	pRetLy = new CC_Layer();
	pRetLy->pAscDraw = pDraw;


	if (pDraw == NULL)
	{
		if (pTpl != NULL)
			{
			pErr->Add(CCERR::EWAR_IN, CCERR::CTX_DRAWINT,
					  TR::tr("Create layer: Layer template may be used only with associated drawing"));

			}
		if (!strNewName.isEmpty())
		{
			pRetLy->str_name = strNewName;
		}
	}
	else
		{
		Q_ASSERT(pDraw->InitOk());

		/* name the thing */
		if (strNewName.isEmpty())
		{
			pRetLy->str_name = uniqName(pDraw,pErr);
			Q_ASSERT(!pRetLy->str_name.isEmpty());
		}
		else
		{
			if (hasLayer(pDraw,strNewName,pErr))
			{
				pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
						  TR::tr("Create layer: Non-unique name provided"));
				delete(pRetLy);
				return bRet;
			}
			pRetLy->str_name = strNewName;
		}

		/* associate the layer with drawing */
		pDraw->Layers.append(pRetLy);

		/* set some form of properties for new layer */
		if (pTpl != NULL)
			{ /* import properties from template */
			pRetLy->grd = pTpl->grd;
			}
		else
			{ /* be shure that it has an graphic object associated */
			bRet = CC_GIP::GetGenericItem(pDraw, &pRetLy->grd, pErr);
			if (!SUCCEDED(bRet))
				{
				pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
						  TR::tr("Create layer: Could not get a graphic object"));
				delete(pRetLy);
				return bRet;
				}
			}

		/* todo signal to interested parties that a change took place in the
	struture of the drawing */

		}

	/* tell the user the output of it's actions */
	if (pOut != NULL)
		*pOut = pRetLy;

	return bRet;
	}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* constructor	                                                             */
CCERR::EERRORS		CC_Layer::bind		(CC_Draw* pDraw, CC_Err* pErr)
	{

	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;

	/* once associated, an layer may not be associated any longer */
	if (pAscDraw != NULL)
		{
		Q_ASSERT(0);
		pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
				  TR::tr("Bind layer: Provided layer is already associated"));
		return CCERR::ERR_STATUS;
		}

	/* only associate with valid drawings */
	if ((pDraw == NULL) || (!pDraw->InitOk()))
		{
		Q_ASSERT(0);
		pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
				  TR::tr("Bind layer: Provided drawing is invalid"));
		return CCERR::ERR_STATUS;
		}

	/* be shure that it has an graphic object associated */
	if (grd == NULL)
		{
		bRet = CC_GIP::GetGenericItem(pDraw, &grd, pErr);
		if (!SUCCEDED(bRet))
			{
			pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
					  TR::tr("Bind layer: Could not get a graphic object"));
			return bRet;
			}
		}

	/* associate the layer with drawing */
	pDraw->Layers.append(this);


	/* todo signal to interested parties that a change took place in the
struture of the drawing */


	return bRet;
	}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
/* destructor	                                                             */
CC_Layer::~CC_Layer()
	{

	}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* function that tells if a specific drawing ha a layer name                 */
bool		CC_Layer::hasLayer		(CC_Draw* pDraw, QString strTst, CC_Err* pErr)
	{
	Q_UNUSED(pErr);

	if (pDraw == NULL)
		{
		Q_ASSERT(0);
		return false;
		}

	if (pDraw->Layers.hasName(strTst))
		return true;

	for (int j = 0; j < pDraw->Spaces.count(); j++)
		{
		CC_DrawSpace* pDS = static_cast<CC_DrawSpace*>(pDraw->Spaces.item(j));
		if (pDS->Layers.hasName(strTst))
			return true;
		}

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* generates a unique name for this layer                                    */
QString		CC_Layer::uniqName		(CC_Draw* pDraw, CC_Err* pErr)
	{
	Q_UNUSED(pErr);

	/* can't get a name without a drawing */
	if (pDraw == NULL)
		{
		Q_ASSERT(0);
		pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
				  TR::tr("Unique layer name: no drawing provided"));
		return QString();
		}

	/* loop-find a proper name */
	unsigned	i = 1;
	QString		str_name;
	while (i < 0xFFFF)
		{
		str_name = TR::tr("Layer %1").arg(i);
		if (!hasLayer(pDraw, str_name, pErr))
			{
			return str_name;
			}
		i++;
		}
	Q_ASSERT(0);
	pErr->Add(CCERR::EERR_IN, CCERR::CTX_DRAWINT,
			  TR::tr("Unique layer name: limit reached"));
	return QString();
	}
/* ========================================================================= */




/* ------------------------------------------------------------------------- */
/* saves the data regarding this class to a stream in CASP_1 format          */
CCERR::EERRORS		CC_Layer::Save_CASP_1	(QDataStream* pStream, CC_Err* pErr)
	{
	Q_UNUSED(pStream);
	Q_UNUSED(pErr);
	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;


	return bRet;
	}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* loads the data regarding this class from a stream in CASP_1 format        */
CCERR::EERRORS		CC_Layer::Load_CASP_1	(QDataStream* pStream, CC_Err* pErr)
	{
	Q_UNUSED(pStream);
	Q_UNUSED(pErr);
	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;


	return bRet;
	}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
/* saves the data regarding this class to a stream in CASP_1 format          */
CCERR::EERRORS		CC_Layer::Save_DXF	(QDataStream* pStream, CC_Err* pErr)
	{
	Q_UNUSED(pStream);
	Q_UNUSED(pErr);
	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;


	return bRet;
	}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* loads the data regarding this class from a stream in CASP_1 format        */
CCERR::EERRORS		CC_Layer::Load_DXF	(QDataStream* pStream, CC_Err* pErr)
	{
	Q_UNUSED(pStream);
	Q_UNUSED(pErr);
	CCERR::EERRORS		bRet = CCERR::ERR_SUCCESS;


	return bRet;
	}
/* ========================================================================= */

/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
